package com.qo.qsdk.utils;

import android.app.Activity;

import java.util.HashMap;

public class PwdManager {

    private static final String TAG = PwdManager.class.getSimpleName();
    private PwdManagerCountdownLister mPwdManagerCountdownLister;
    private HashMap<Integer,Integer> mCountdownMap = new HashMap<>();
    private int Max_Count = 2;
    private int mCurrentCount = 0;
    private int mCurrentLevel = 1;
    private long endTime = 0;
    private long lastTime = 0;
    private final String KEY = "PwdManager";

    public void setPwdManagerCountdownLister(PwdManagerCountdownLister mPwdManagerCountdownLister) {
        this.mPwdManagerCountdownLister = mPwdManagerCountdownLister;
    }

    private static class AppManagerHolder {
        private static final PwdManager instance_ = new PwdManager();
    }

    public static PwdManager getInstance() {
        return AppManagerHolder.instance_;
    }

    public PwdManager() {
        mCountdownMap.put(1, 0);
        mCountdownMap.put(2, 60 * 1000);//毫秒
        mCountdownMap.put(3, 600 * 1000);
        mCountdownMap.put(4, 60 * 60 * 2 * 1000);
        mCountdownMap.put(5, 60 * 60 * 24 * 1000);

    }

    public void init(Activity activity) {
        Object time = QSDKHelper.getInstance().getItem(activity, KEY, Long.parseLong("0"));
        if (time != null) {
            endTime = (long) time;
        }else{
            endTime = 0;
        }
    }

    public void countDown(Activity activity) {
        if (lastTime > 0) {
            if (System.currentTimeMillis() - lastTime < 60 * 1000) {
                mCurrentCount += 1;
                if (mCurrentCount >= Max_Count) {
                    mCurrentLevel += 1;
                    mCurrentCount = 0;
                    if (mCountdownMap.get(mCurrentLevel) != null) {
                        endTime = mCountdownMap.get(mCurrentLevel) + (System.currentTimeMillis());
                        QSDKHelper.getInstance().setItem(activity, KEY, endTime);
                    }
                    if (mPwdManagerCountdownLister != null) mPwdManagerCountdownLister.start(mCountdownMap.get(mCurrentLevel));
                    lastTime = 0;
                }
                return;
            }else{
                mCurrentCount = 0;
            }
        }
        lastTime = System.currentTimeMillis();
    }

    public String displayTime() {
        //每天毫秒数
        long nd = 1000 * 24 * 60 * 60;
        //每小时毫秒数
        long nh = 1000 * 60 * 60;
        //每分钟毫秒数
        long nm = 1000 * 60;

        long ns = 1000;

        long diff = getTime();

        if (diff == 0) {
            return "";
        }

        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        long sec = diff % nd % nh % nm / ns;

        String result = "";
        if (day > 0) {
            result += day + Language.Pwd_Day.toString();
        }
        if (hour > 0) {
            result += hour + Language.Pwd_Hour.toString();
        }
        if (min > 0) {
            result += min + Language.Pwd_Minute.toString();
        }
        if (sec > 0) {
            result += sec + Language.Pwd_Second.toString();
        }

        return result;
    }

    public int getTime() {
        return (int)Math.max(0, endTime - (System.currentTimeMillis()));
    }

    public interface PwdManagerCountdownLister {
        void update(int time);
        void start(int time);
    }
}
